import 'package:flutter/material.dart';
import 'package:lyf_shop/modules/login/views/hi_login_navbar.dart';
import 'package:lyf_shop/modules/login/views/login_effect.dart';
import 'package:lyf_shop/modules/login/views/login_input.dart';
import 'package:lyf_shop/utils/hi_string_util.dart';

class LoginPage extends StatefulWidget {
  const LoginPage({super.key});

  @override
  State<LoginPage> createState() => _LoginPageState();
}

class _LoginPageState extends State<LoginPage> {
  bool protect = false;
  bool loginEnable = false;
  String? userName;
  String? password;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: HiAppBar('密码登录', '注册', () {}),
      body: Container(
        child: ListView(
          children: [
            LoginEffect(protect: protect),
            LoginInput(
                title: '用户名',
                hint: '请输入用户名',
                onChanged: (value) {
                  userName = value;
                  checkInput();
                }),
            LoginInput(
              title: '密码',
              hint: '请输入密码',
              onChanged: (value) {
                password = value;
                checkInput();
              },
              focusChanged: (focus) {
                this.setState(() {
                  protect = focus;
                });
              },
            ),
            Padding(
              padding: EdgeInsets.only(top: 40, left: 20, right: 20),
              child: FractionallySizedBox(
                widthFactor: 1,
                child: MaterialButton(
                  shape: RoundedRectangleBorder(
                      borderRadius: BorderRadius.circular(6)),
                  height: 45,
                  onPressed: loginEnable ? _loginBtnClicked : null,
                  disabledColor: Colors.grey,
                  color: Colors.pink,
                  child: Text(
                    '登录',
                    style: TextStyle(fontSize: 16, color: Colors.white),
                  ),
                ),
              ),
            )
          ],
        ),
      ),
    );
  }

  _loginBtnClicked() {}

  void checkInput() {
    bool enable;
    if (isNotEmpty(userName) && isNotEmpty(password)) {
      enable = true;
    } else {
      enable = false;
    }

    setState(() {
      loginEnable = enable;
    });
  }
}
